//-----------------------------------------------------------------------
// <copyright file="a_CouponDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_CouponDAOBase
    {
        #region Common methods
        public virtual a_Coupon Createa_CouponFromReader(IDataReader reader)
        {
            a_Coupon item = new a_Coupon();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("CouponID"))) item.CouponID = (int)reader["CouponID"];
                if (!reader.IsDBNull(reader.GetOrdinal("RedemptionCode"))) item.RedemptionCode = (string)reader["RedemptionCode"];
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) item.Name = (string)reader["Name"];
                if (!reader.IsDBNull(reader.GetOrdinal("Note"))) item.Note = (string)reader["Note"];
                if (!reader.IsDBNull(reader.GetOrdinal("CouponType"))) item.CouponType = (byte)reader["CouponType"];
                if (!reader.IsDBNull(reader.GetOrdinal("RecipientEmail"))) item.RecipientEmail = (string)reader["RecipientEmail"];
                if (!reader.IsDBNull(reader.GetOrdinal("DateCreated"))) item.DateCreated = (DateTime)reader["DateCreated"];
                if (!reader.IsDBNull(reader.GetOrdinal("DateStart"))) item.DateStart = (DateTime)reader["DateStart"];
                if (!reader.IsDBNull(reader.GetOrdinal("DateEnd"))) item.DateEnd = (DateTime)reader["DateEnd"];
                if (!reader.IsDBNull(reader.GetOrdinal("EligibleType"))) item.EligibleType = (byte)reader["EligibleType"];
                if (!reader.IsDBNull(reader.GetOrdinal("Req_MinOrderAmt"))) item.Req_MinOrderAmt = (decimal)reader["Req_MinOrderAmt"];
                if (!reader.IsDBNull(reader.GetOrdinal("Req_MinItemQty"))) item.Req_MinItemQty = (int)reader["Req_MinItemQty"];
                if (!reader.IsDBNull(reader.GetOrdinal("MinQtyType"))) item.MinQtyType = (bool)reader["MinQtyType"];
                if (!reader.IsDBNull(reader.GetOrdinal("Req_Extended"))) item.Req_Extended = (bool)reader["Req_Extended"];
                if (!reader.IsDBNull(reader.GetOrdinal("CouponValueType"))) item.CouponValueType = (bool)reader["CouponValueType"];
                if (!reader.IsDBNull(reader.GetOrdinal("CouponValue"))) item.CouponValue = (int)reader["CouponValue"];
                if (!reader.IsDBNull(reader.GetOrdinal("ApplyTo"))) item.ApplyTo = (bool)reader["ApplyTo"];
                if (!reader.IsDBNull(reader.GetOrdinal("Combinable"))) item.Combinable = (bool)reader["Combinable"];
                if (!reader.IsDBNull(reader.GetOrdinal("IncOnSpecials"))) item.IncOnSpecials = (bool)reader["IncOnSpecials"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsDelete"))) item.IsDelete = (bool)reader["IsDelete"];
                if (!reader.IsDBNull(reader.GetOrdinal("Status"))) item.Status = (byte)reader["Status"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsLock"))) item.IsLock = (bool)reader["IsLock"];
                if (!reader.IsDBNull(reader.GetOrdinal("DomainID"))) item.DomainID = (int)reader["DomainID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Limitfirstper"))) item.Limitfirstper = (int)reader["Limitfirstper"];
                if (!reader.IsDBNull(reader.GetOrdinal("Limitmaxqty"))) item.Limitmaxqty = (int)reader["Limitmaxqty"];
                if (!reader.IsDBNull(reader.GetOrdinal("Limitmaxpur"))) item.Limitmaxpur = (int)reader["Limitmaxpur"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsReq_MinOrderAmt"))) item.IsReq_MinOrderAmt = (bool)reader["IsReq_MinOrderAmt"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsReq_MinItemQty"))) item.IsReq_MinItemQty = (bool)reader["IsReq_MinItemQty"];
                if (!reader.IsDBNull(reader.GetOrdinal("Islimitfirstper"))) item.Islimitfirstper = (bool)reader["Islimitfirstper"];
                if (!reader.IsDBNull(reader.GetOrdinal("Countfirstper"))) item.Countfirstper = (int)reader["Countfirstper"];
                if (!reader.IsDBNull(reader.GetOrdinal("Islimitmaxqty"))) item.Islimitmaxqty = (bool)reader["Islimitmaxqty"];
                if (!reader.IsDBNull(reader.GetOrdinal("Islimitmaxpur"))) item.Islimitmaxpur = (bool)reader["Islimitmaxpur"];
                if (!reader.IsDBNull(reader.GetOrdinal("FreeShippingService"))) item.FreeShippingService = (string)reader["FreeShippingService"];
                if (!reader.IsDBNull(reader.GetOrdinal("FreeTax"))) item.FreeTax = (bool)reader["FreeTax"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsExpired"))) item.IsExpired = (bool)reader["IsExpired"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Coupon methods
            
        public virtual void Createa_Coupon(a_Coupon a_Coupon)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Coupons_ins");
                
                database.AddInParameter(dbCommand, "@RedemptionCode", DbType.AnsiString, a_Coupon.RedemptionCode);
                database.AddInParameter(dbCommand, "@Name", DbType.AnsiString, a_Coupon.Name);
                database.AddInParameter(dbCommand, "@Note", DbType.AnsiString, a_Coupon.Note);
                database.AddInParameter(dbCommand, "@CouponType", DbType.Byte, a_Coupon.CouponType);
                database.AddInParameter(dbCommand, "@RecipientEmail", DbType.AnsiString, a_Coupon.RecipientEmail);
                database.AddInParameter(dbCommand, "@DateCreated", DbType.DateTime, a_Coupon.DateCreated);
                database.AddInParameter(dbCommand, "@DateStart", DbType.DateTime, a_Coupon.DateStart);
                database.AddInParameter(dbCommand, "@DateEnd", DbType.DateTime, a_Coupon.DateEnd);
                database.AddInParameter(dbCommand, "@EligibleType", DbType.Byte, a_Coupon.EligibleType);
                database.AddInParameter(dbCommand, "@Req_MinOrderAmt", DbType.Currency, a_Coupon.Req_MinOrderAmt);
                database.AddInParameter(dbCommand, "@Req_MinItemQty", DbType.Int32, a_Coupon.Req_MinItemQty);
                database.AddInParameter(dbCommand, "@MinQtyType", DbType.Boolean, a_Coupon.MinQtyType);
                database.AddInParameter(dbCommand, "@Req_Extended", DbType.Boolean, a_Coupon.Req_Extended);
                database.AddInParameter(dbCommand, "@CouponValueType", DbType.Boolean, a_Coupon.CouponValueType);
                database.AddInParameter(dbCommand, "@CouponValue", DbType.Int32, a_Coupon.CouponValue);
                database.AddInParameter(dbCommand, "@ApplyTo", DbType.Boolean, a_Coupon.ApplyTo);
                database.AddInParameter(dbCommand, "@Combinable", DbType.Boolean, a_Coupon.Combinable);
                database.AddInParameter(dbCommand, "@IncOnSpecials", DbType.Boolean, a_Coupon.IncOnSpecials);
                database.AddInParameter(dbCommand, "@IsDelete", DbType.Boolean, a_Coupon.IsDelete);
                database.AddInParameter(dbCommand, "@Status", DbType.Byte, a_Coupon.Status);
                database.AddInParameter(dbCommand, "@IsLock", DbType.Boolean, a_Coupon.IsLock);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_Coupon.DomainID);
                database.AddInParameter(dbCommand, "@Limitfirstper", DbType.Int32, a_Coupon.Limitfirstper);
                database.AddInParameter(dbCommand, "@Limitmaxqty", DbType.Int32, a_Coupon.Limitmaxqty);
                database.AddInParameter(dbCommand, "@Limitmaxpur", DbType.Int32, a_Coupon.Limitmaxpur);
                database.AddInParameter(dbCommand, "@IsReq_MinOrderAmt", DbType.Boolean, a_Coupon.IsReq_MinOrderAmt);
                database.AddInParameter(dbCommand, "@IsReq_MinItemQty", DbType.Boolean, a_Coupon.IsReq_MinItemQty);
                database.AddInParameter(dbCommand, "@Islimitfirstper", DbType.Boolean, a_Coupon.Islimitfirstper);
                database.AddInParameter(dbCommand, "@Countfirstper", DbType.Int32, a_Coupon.Countfirstper);
                database.AddInParameter(dbCommand, "@Islimitmaxqty", DbType.Boolean, a_Coupon.Islimitmaxqty);
                database.AddInParameter(dbCommand, "@Islimitmaxpur", DbType.Boolean, a_Coupon.Islimitmaxpur);
                database.AddInParameter(dbCommand, "@FreeShippingService", DbType.AnsiString, a_Coupon.FreeShippingService);
                database.AddInParameter(dbCommand, "@FreeTax", DbType.Boolean, a_Coupon.FreeTax);
                database.AddInParameter(dbCommand, "@IsExpired", DbType.Boolean, a_Coupon.IsExpired);
                database.AddOutParameter(dbCommand, "@CouponID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_Coupon.CouponID = (int)database.GetParameterValue(dbCommand, "@CouponID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Coupon methods
        
        public virtual void Updatea_Coupon(a_Coupon a_Coupon)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Coupons_upd");            
                
                database.AddInParameter(dbCommand, "@CouponID", DbType.Int32, a_Coupon.CouponID);
                database.AddInParameter(dbCommand, "@RedemptionCode", DbType.AnsiString, a_Coupon.RedemptionCode);
                database.AddInParameter(dbCommand, "@Name", DbType.AnsiString, a_Coupon.Name);
                database.AddInParameter(dbCommand, "@Note", DbType.AnsiString, a_Coupon.Note);
                database.AddInParameter(dbCommand, "@CouponType", DbType.Byte, a_Coupon.CouponType);
                database.AddInParameter(dbCommand, "@RecipientEmail", DbType.AnsiString, a_Coupon.RecipientEmail);
                database.AddInParameter(dbCommand, "@DateCreated", DbType.DateTime, a_Coupon.DateCreated);
                database.AddInParameter(dbCommand, "@DateStart", DbType.DateTime, a_Coupon.DateStart);
                database.AddInParameter(dbCommand, "@DateEnd", DbType.DateTime, a_Coupon.DateEnd);
                database.AddInParameter(dbCommand, "@EligibleType", DbType.Byte, a_Coupon.EligibleType);
                database.AddInParameter(dbCommand, "@Req_MinOrderAmt", DbType.Currency, a_Coupon.Req_MinOrderAmt);
                database.AddInParameter(dbCommand, "@Req_MinItemQty", DbType.Int32, a_Coupon.Req_MinItemQty);
                database.AddInParameter(dbCommand, "@MinQtyType", DbType.Boolean, a_Coupon.MinQtyType);
                database.AddInParameter(dbCommand, "@Req_Extended", DbType.Boolean, a_Coupon.Req_Extended);
                database.AddInParameter(dbCommand, "@CouponValueType", DbType.Boolean, a_Coupon.CouponValueType);
                database.AddInParameter(dbCommand, "@CouponValue", DbType.Int32, a_Coupon.CouponValue);
                database.AddInParameter(dbCommand, "@ApplyTo", DbType.Boolean, a_Coupon.ApplyTo);
                database.AddInParameter(dbCommand, "@Combinable", DbType.Boolean, a_Coupon.Combinable);
                database.AddInParameter(dbCommand, "@IncOnSpecials", DbType.Boolean, a_Coupon.IncOnSpecials);
                database.AddInParameter(dbCommand, "@IsDelete", DbType.Boolean, a_Coupon.IsDelete);
                database.AddInParameter(dbCommand, "@Status", DbType.Byte, a_Coupon.Status);
                database.AddInParameter(dbCommand, "@IsLock", DbType.Boolean, a_Coupon.IsLock);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_Coupon.DomainID);
                database.AddInParameter(dbCommand, "@Limitfirstper", DbType.Int32, a_Coupon.Limitfirstper);
                database.AddInParameter(dbCommand, "@Limitmaxqty", DbType.Int32, a_Coupon.Limitmaxqty);
                database.AddInParameter(dbCommand, "@Limitmaxpur", DbType.Int32, a_Coupon.Limitmaxpur);
                database.AddInParameter(dbCommand, "@IsReq_MinOrderAmt", DbType.Boolean, a_Coupon.IsReq_MinOrderAmt);
                database.AddInParameter(dbCommand, "@IsReq_MinItemQty", DbType.Boolean, a_Coupon.IsReq_MinItemQty);
                database.AddInParameter(dbCommand, "@Islimitfirstper", DbType.Boolean, a_Coupon.Islimitfirstper);
                database.AddInParameter(dbCommand, "@Countfirstper", DbType.Int32, a_Coupon.Countfirstper);
                database.AddInParameter(dbCommand, "@Islimitmaxqty", DbType.Boolean, a_Coupon.Islimitmaxqty);
                database.AddInParameter(dbCommand, "@Islimitmaxpur", DbType.Boolean, a_Coupon.Islimitmaxpur);
                database.AddInParameter(dbCommand, "@FreeShippingService", DbType.AnsiString, a_Coupon.FreeShippingService);
                database.AddInParameter(dbCommand, "@FreeTax", DbType.Boolean, a_Coupon.FreeTax);
                database.AddInParameter(dbCommand, "@IsExpired", DbType.Boolean, a_Coupon.IsExpired);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Coupon methods
        public virtual void Deletea_Coupon(int couponID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Coupons_del");
                
                database.AddInParameter(dbCommand, "@CouponID", DbType.Int32, couponID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Coupon methods
        
        public virtual a_Coupon Geta_Coupon(int couponID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Coupons_sel");
                
                database.AddInParameter(dbCommand, "@CouponID", DbType.Int32, couponID);
                
                a_Coupon a_Coupon =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Coupon = Createa_CouponFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Coupon;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_CouponList methods
        public virtual a_CouponCollection Geta_CouponList(a_CouponColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Coupons_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_CouponCollection a_CouponCollection = new a_CouponCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Coupon a_Coupon = Createa_CouponFromReader(reader);
                        a_CouponCollection.Add(a_Coupon);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_CouponCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_CouponCollection Geta_CouponList(a_CouponColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_CouponList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}